import { http } from '@/utils/request';
import type { ApiResponse } from '@/types';

// 公告相关类型定义
export interface Announcement {
  id: number;
  title: string;
  content: string;
  type: string;
  priority: number;
  is_active: boolean;
  created_at: string;
  updated_at?: string;
  author: {
    id: number;
    username: string;
    avatar: string;
  };
}

export interface AnnouncementListResponse {
  announcements: Announcement[];
  pagination: {
    page: number;
    page_size: number;
    total: number;
    total_page: number;
  };
}

export interface AnnouncementQuery {
  page?: number;
  page_size?: number;
  type?: string;
  active?: boolean;
}

export interface CreateAnnouncementRequest {
  title: string;
  content: string;
  type?: string;
  priority?: number;
}

export interface UpdateAnnouncementRequest {
  title?: string;
  content?: string;
  type?: string;
  priority?: number;
  is_active?: boolean;
}

export const announcementApi = {
  // 获取公告列表
  getAnnouncements(params?: AnnouncementQuery): Promise<ApiResponse<AnnouncementListResponse>> {
    return http.get('/v1/announcements', { params });
  },

  // 获取公告详情
  getAnnouncementDetail(announcementId: number): Promise<ApiResponse<Announcement>> {
    return http.get(`/v1/announcements/${announcementId}`);
  },

  // 创建公告
  createAnnouncement(data: CreateAnnouncementRequest): Promise<ApiResponse<{ id: number }>> {
    return http.post('/v1/announcements', data);
  },

  // 更新公告
  updateAnnouncement(announcementId: number, data: UpdateAnnouncementRequest): Promise<ApiResponse<void>> {
    return http.put(`/v1/announcements/${announcementId}`, data);
  },

  // 删除公告
  deleteAnnouncement(announcementId: number): Promise<ApiResponse<void>> {
    return http.delete(`/v1/announcements/${announcementId}`);
  },
};
